/* -----------------------------------------------------------------------------
 * PROJECT    : lwf
 * FILE       : UserService.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Mar 1, 2011  mycodetalks			       Created
 */
package net.lwf.service;

import net.lwf.hibernate.dao.UserDAO;
import net.lwf.hibernate.entity.User;
import net.lwf.security.EncryptDecryptor;
import net.lwf.user.fake.service.MockUserService;

/**
 * Class description goes here
 */
public class UserService {

	public User getUser(String userid, String password) {
		User user = null;
		if (userid != null && password != null) {
			if (LwfService.isMock()) {
				user = this.getMockUser(userid, password);
			}
			else {
				UserDAO dao = new UserDAO();
				user = dao.getUser(userid);
			}
		}
		if (user != null) {
//			PasswordEncryptor encryptor = new BasicPasswordEncryptor();
//			if (encryptor.checkPassword(password, user.getPassword())
//					&& user.getUserStatus().equals("ACTIVE")) {
//				user.setValid(true);
//			}
			EncryptDecryptor encryptdecrypt = new EncryptDecryptor();
			if(encryptdecrypt.isValid(password, user.getPassword(), EncryptDecryptor.BASIC_PWD) && 
			   user.getUserStatus().equals("ACTIVE")) {
				user.setValid(true);
			}
		}

		return user;
	}
	
	public boolean isPrimaryUser(String userid, String password) {
		if (userid != null && password != null) {
			if (LwfService.isMock()) {
				return true;
			}
			else {
				UserDAO dao = new UserDAO();
				User user = dao.getUser(userid);
				if (user != null) {
					EncryptDecryptor encryptdecrypt = new EncryptDecryptor();
					if(encryptdecrypt.isValid(password, user.getPassword(), EncryptDecryptor.BASIC_PWD) && 
					   user.getUserStatus().equals("ACTIVE")) {
						return true;
					}
				}
			}	
		}
		return false;
	}
	
/*	public long getHitCounter() {
		if (LwfService.isMock()) {
			return 1;
		}
		return new AppHitDAO().getHitCounter();
	}
	
	public void logHit(long counter) {
		if (LwfService.isMock()) {
			return;
		}
		new AppHitDAO().logHit(counter);
	}*/
	
	//---------------------------------------------------------------------------------------------
	// Mock Data retrieval methods
	//---------------------------------------------------------------------------------------------

	private User getMockUser(String userid, String password) {
		User user = null;
		MockUserService service = new MockUserService();
		net.lwf.user.fake.jaxb.User mockuser = service
				.getUser(userid, password);
		if (mockuser != null) {
			user = new User();
			user.setId(Long.parseLong(mockuser.getId()));
			user.setGrade(mockuser.getGrade());
			user.setFirstName(mockuser.getFirstName());
			user.setLastName(mockuser.getLastName());
			user.setUserId(mockuser.getUserId());
			user.setPassword(mockuser.getPassword());
			user.setUserStatus(mockuser.getUserStatus());
			user.setUserType(mockuser.getUserType());
		}
		return user;
	}

}



